// mircoApp/idShop/idShop_apply/idShop_apply.js
const app = getApp();
import utils from "../../../utils/util";
var location = require("../../../mybehaviors/location/location");

Page({
  behaviors: [location],
  /**
   * 页面的初始数据
   */
  data: {
    tabActive: 1,
    radio: "1",
    radioType: "1",
    idPopupShow: false,
    inviterPopupShow: false,
    // 子id号
    childId: "",
    // 邀请人手机号
    inviterMobile: "",
    wise_man: {},
    red_man: {},
    city: "",
    adcode: "",
    chilId_info: null,
    timer: '',
    applyStatus: 0,
    wise_man_button:'',
    red_man_button :''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.tabActive) {
      this.setData({
        tabActive: options.tabActive
      })
    }
    this._getLocation((res) => {
      let address_on = wx.getStorageSync('address-point');
      this.setData({
        adcode: address_on.adcode,
        city: `${address_on.city}${address_on.address_component.street_number}`
      })
    }, (err) => {});
    this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      idPopupShow:false,
      childId:"",
      chilId_info:null
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  chilGetId(e) {
    let val = e.detail.value;
    this.setData({
      childId: val
    })
    // 防抖
    if (this.data.timer) clearTimeout(this.data.timer);
    this.data.timer = setTimeout(() => {
      utils.debounce(this.getChild());
    }, 600);
  },
  wise_manBuyGood() {
    if (!this.data.wise_man.goods.status) {
      wx.navigateTo({
        url: '/packageA/detail_v2/detail_v2?id=' + this.data.wise_man.goods.goods_id,
      })
    }
  },
  red_manBuyGood() {
    if (!this.data.red_man.goods.status) {
      wx.navigateTo({
        url: '/packageA/detail_v2/detail_v2?id=' + this.data.red_man.goods.goods_id,
      })
    }
  },
  invitInp(e) {
    this.setData({
      inviterMobile: e.detail.value
    })
  },
  onChange(e) {
    this.setData({
      radio: e.detail
    })
  },
  onChangeType(e) {
    this.setData({
      radioType: e.detail
    })
  },
  tabActiveChange(e) {
    let {
      key
    } = e.currentTarget.dataset;
    this.setData({
      tabActive: key
    })
  },
  inviterPopupShowOpen(e) {
    this.setData({
      inviterPopupShow: true
    })
  },
  onClickLeft(e) {
    this.setData({
      inviterPopupShow: false,
      idPopupShow: false
    })
  },
  idPopupShow_open(e) {
    if (this.data.radio == 2) {
      if (!this.data.red_man.auth.company) {
        app.tips("请先进行企业认证才可购买子id")
        return;
      }
    } else {
      if (!this.data.red_man.auth.person) {
        app.tips("请先进行个人认证才可购买子id")
        return;
      }
    }
    this.setData({
      idPopupShow: true
    })
  },
  toCompanyApply() {
    wx.navigateTo({
      url: '/mircoApp/idShop/idShop_apply_enterprise/idShop_apply_enterprise',
    })
  },
  toPersonAuth(){
    wx.navigateTo({
      url: '/packageH/signPage/personBank/personBank?from=bindRealName',
    })
  },
  async onClickRight() {
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.project-party.frontend.controllers.apply.checkInviter"),
      data: {
        mobile: this.data.inviterMobile,
        man_type:this.data.tabActive == 1 ? 2 : 1,
      }
    })
    app.tips(data.msg);
    if (data.result) {
      this.setData({
        inviterPopupShow: false
      })
      this.getData()
    }
  },
  async getChild() {
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.project-party.frontend.controllers.apply.checkChildId"),
      data: {
        child_id: this.data.childId
      }
    })
    if (data.result) {
      this.setData({
        chilId_info: data.data,
      })
    } else {
      this.setData({
        chilId_info: null
      })
    }
  },
  async getData(key = false) {
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.project-party.frontend.controllers.apply.index"),
    })
    if (!data.result) {
      app.tips(data.msg)
      return
    }
    let res = data;
    this.setData({
      red_man: res.data.red_man,
      wise_man: res.data.wise_man,
      image_url: res.data.image_url,
      applyStatus: res.data.status,
      wise_man_button:res.data.wise_man_button,
      red_man_button:res.data.red_man_button
    })
    if (key) return;
    if (this.data.red_man?.auth?.company) {
      this.data.radio = "2";
    } else {
      this.data.radio = "1";
    }
    if (this.data.red_man?.goods.status == 1) {
      this.data.radioType = "1";
    } else if (this.data.red_man?.is_buy_child_id) {
      this.data.radioType = "2";
    } else {
      this.data.radioType = "1";
    }
    this.setData({
      radioType: this.data.radioType,
      radio: this.data.radio
    })
  },
  tapApplyB() {
    if (this.data.tabActive == 1) {
      // 红人申请
      this.redApply();
    } else {
      //达人申请
      this.wiseApply();
    }
  },
  async redApply() {
    let json = {
      form: {
        inviter_tel: this.data.inviterMobile,
        auth_type: this.data.radio,
        product_type: this.data.radioType,
        district_id: this.data.adcode?this.data.adcode:0
      }
    };
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.project-party.frontend.controllers.apply.applyRed"),
      data: json
    })
    if (data.result) {
      app.tips(data.msg)
      await this.getData();
      wx.navigateTo({
        url: '/mircoApp/idShop/myWallet/index',
      })
    }else{
      app.tips(data.msg);
    }
  },
  async wiseApply() {
    let json = {
      form: {
        inviter_tel: this.data.inviterMobile,
        district_id: this.data.adcode?this.data.adcode:0
      }
    };
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.project-party.frontend.controllers.apply.applyWise"),
      data: json
    })
    if (data.result) {
      app.tips(data.msg)
      await this.getData();
      wx.navigateTo({
        url: '/mircoApp/idShop/myWallet/index',
      })
    }else{
      app.tips(data.msg);
    }
  },
  errLoaction() {
    if (!this.data.city) {
      wx.navigateTo({
        url: '/packageC/o2o/o2oLocation_loc/o2oLocation_loc?tag=idShopApply',
      })
      return;
    }
  },
  async onClickRight_id() {
    this.setData({
      idPopupShow: false
    })
    let {
      pay_sn
    } = await this.getOrderSn();
    let _seatChildId_json = {
      pay_sn: pay_sn,
      child_id: this.data.chilId_info.child_id
    };
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.project-party.frontend.controllers.child_id.seatChildId"),
      data:_seatChildId_json
    })
    if(!data.result){
      app.tips(data.msg);
      return
    }
    wx.navigateTo({
      url: `/packageD/buy/orderPay/orderPay?tag=idShopApply&price=${Number(data.data.price)}&child_id=${this.data.chilId_info.child_id}&ordersn=${pay_sn}`,
    })
  },
  async getOrderSn() {
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.project-party.frontend.controllers.child-id.getPaySn")
    })
    return data.data
  }
})